<template>
    <el-dialog
            :title="!dataForm.id ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
                 label-width="80px">
            <el-form-item label="评论类型 - 0、留言；1、文章评论" prop="type">
                <el-input v-model="dataForm.type" placeholder="评论类型 - 0、留言；1、文章评论"></el-input>
            </el-form-item>
            <el-form-item label="父级id，楼主评论，-1" prop="parentId">
                <el-input v-model="dataForm.parentId" placeholder="父级id，楼主评论，-1"></el-input>
            </el-form-item>
            <el-form-item label="评价对象id，对应 type=1" prop="contentId">
                <el-input v-model="dataForm.contentId" placeholder="评价对象id，对应 type=1"></el-input>
            </el-form-item>
            <el-form-item label="言论，评价内容" prop="word">
                <el-input v-model="dataForm.word" placeholder="言论，评价内容"></el-input>
            </el-form-item>
            <el-form-item label="是否展示" prop="isShow">
                <el-input v-model="dataForm.isShow" placeholder="是否展示"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data() {
            return {
                visible: false,
                dataForm: {
                    id: 0,
                    type: '',
                    parentId: '',
                    contentId: '',
                    word: '',
                    isShow: '',
                    createBy: '',
                    createTime: '',
                    lastUpdateBy: '',
                    lastUpdateTime: ''
                },
                dataRule: {
                    type: [
                        {required: true, message: '评论类型 - 0、留言；1、文章评论不能为空', trigger: 'blur'}
                    ],
                    parentId: [
                        {required: true, message: '父级id，楼主评论，-1不能为空', trigger: 'blur'}
                    ],
                    contentId: [
                        {required: true, message: '评价对象id，对应 type=1不能为空', trigger: 'blur'}
                    ],
                    word: [
                        {required: true, message: '言论，评价内容不能为空', trigger: 'blur'}
                    ],
                    isShow: [
                        {required: true, message: '是否展示不能为空', trigger: 'blur'}
                    ],
                    createBy: [
                        {required: true, message: '创建人不能为空', trigger: 'blur'}
                    ],
                    createTime: [
                        {required: true, message: '创建时间不能为空', trigger: 'blur'}
                    ],
                    lastUpdateBy: [
                        {required: true, message: '更新人不能为空', trigger: 'blur'}
                    ],
                    lastUpdateTime: [
                        {required: true, message: '更新时间不能为空', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.id = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.id) {
                        this.$api.Issue.info(this.dataForm.id).then((data) => {
                            if (data && data.code === 0) {
                                this.dataForm.type = data.issue.type
                                this.dataForm.parentId = data.issue.parentId
                                this.dataForm.contentId = data.issue.contentId
                                this.dataForm.word = data.issue.word
                                this.dataForm.isShow = data.issue.isShow
                                this.dataForm.createBy = data.issue.createBy
                                this.dataForm.createTime = data.issue.createTime
                                this.dataForm.lastUpdateBy = data.issue.lastUpdateBy
                                this.dataForm.lastUpdateTime = data.issue.lastUpdateTime
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {

                        let data = {
                            'id': this.dataForm.id || undefined,
                            'type': this.dataForm.type,
                            'parentId': this.dataForm.parentId,
                            'contentId': this.dataForm.contentId,
                            'word': this.dataForm.word,
                            'isShow': this.dataForm.isShow,
                            'createBy': this.dataForm.createBy,
                            'createTime': this.dataForm.createTime,
                            'lastUpdateBy': this.dataForm.lastUpdateBy,
                            'lastUpdateTime': this.dataForm.lastUpdateTime
                        };
                        let resultAction;
                        if (!this.dataForm.id) {
                            resultAction = this.$api.Issue.save(data);
                        } else {
                            resultAction = this.$api.Issue.update(data);
                        }

                        resultAction.then((data) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        },
    }
</script>
